package com.kingtone.rongji.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.kingtone.rongji.biz.Product;

public class CartUtils {
	private static final String SP_NAME = "carts";
	
	public static void add2Cart(Context context, Product item) {
		if(null == item || null == context || item.getProductId().length() == 0) {
			return;
		}
		
		// 默认1
		if(item.getQuantity()==0) {
			item.setQuantity(1);
		}
		
		
		// 储存数据
		String pid = item.getProductId() + "";
		String oldItem = get(context, pid);
		
		if(!TextUtils.isEmpty(oldItem)) {
			Product product = json2Obj(oldItem);
			item.setQuantity(product.getQuantity()+1);
		}
		
		String old = obj2Json(item);
		save(context, pid, old);
	}
	
	// 删除产品对应一条数据
	public static void deleteFromCart(Context context, Product item) {
		if(null == item || null == context || item.getProductId().length() == 0) {
			return;
		}
		
		String pid = item.getProductId() + "";
		String old = get(context, pid);
		if(!TextUtils.isEmpty(old)) {
			Product product = json2Obj(old);
			product.setQuantity(product.getQuantity()-1);
			if(product.getQuantity()>0) {
				old = obj2Json(product);
				save(context, pid, old);
			} else {
				save(context, pid, "");
			}
		}
	}
	
	// 删除产品所有数据
	public static void deleteProduct(Context context, Product item) {
		if(null == item || null == context || item.getProductId().length() == 0) {
			return;
		}
		
		String pid = item.getProductId() + "";
		save(context, pid, "");
	}
	
	// 删除一组产品
	public static void deleteProductList(Context context, List<Product> list) {
		for(Product item : list) {
			deleteProduct(context, item);
		}
	}
	
	// 获取所有数据
	public static List<Product> getAllProductsForCart(Context context) {
		List<Product> result = new ArrayList<Product>();
		List<String> data = getAll(context);
		for(String item : data) {
			if(!TextUtils.isEmpty(item)) {
				result.add(json2Obj(item));
			}
		}
		return result;
	}
	
	// 清空购物车
	public static void clearCart(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}
	
	private static String obj2Json(Product obj) {
		Gson gson = new Gson();
		String result = gson.toJson(obj);
		return result;
	}
	
	private static Product json2Obj(String json) {
		Gson gson = new Gson();
		Product result = gson.fromJson(json, Product.class);
		return result;
	}
	
	private static void save(Context context, String pid, String item) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(pid, item);
		editor.commit();
	}
	
	private static String get(Context context, String pid) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String result = sp.getString(pid, "");
		return result;
	}
	
	private static List<String> getAll(Context context) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		Map<String, ?> datas = sp.getAll();
		List<String> result = new ArrayList<String>();
		for(Object item : datas.values()) {
			result.add(item.toString());
		}
		return result;
	}
	
	public static int getCartCounts(Context context)
	{
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		Map<String, ?> datas = sp.getAll();
		return datas.size();
	}
}
